﻿using System;
using UFrame;
using UnityEngine;

public class CameraLimit : MonoBehaviour
{
    public Camera mainCamera;

    public bool isLimitPosition = false;
    public bool isLimitRotation = true;
    public bool isUpdate = false;
    public bool isDebug = false;

    //旋转相关
    public int yMinLimit = -20;
    public int yMaxLimit = 80;
    public int xMinLimit = -20;
    public int xMaxLimit = 80;

    [HideInInspector] public float yMinLimitTarget;
    [HideInInspector] public float yMaxLimitTarget;
    [HideInInspector] public float xMinLimitTarget;
    [HideInInspector] public float xMaxLimitTarget;

    //旋转速度
    [Header("镜头移动速度X")] public float xSpeed = 30f;
    [Header("镜头移动速度Y")] public float ySpeed = 30f;

    [Range(0.1f, 1f)] public float slerpValue = 0.1f;

    //旋转角度
    private float addUpX = 0.0f;
    private float addUpY = 0.0f;
    private float oldX = 0.0f;
    private float oldY = 0.0f;
    private float originFlow;
    private float accuX; //
    private float accuY; //

    [HideInInspector] public Quaternion curRot = Quaternion.identity;
    [HideInInspector] public Vector3 curPos = Vector3.zero;

    private bool isLockUpdate = false;

    public bool isMouseInput = false;
    public bool isTouchInput = false;

    private Vector2 interpolation;
    [Range(0, 30f)] public float rotateSpeed = 0.5f;
    
    [Range(0, 10f),Header("忽略距离")] public float ignoreDistance = 0.1f;
    public float maximumRot = 0.05f;
    private Vector2 lastPos;
    private Vector2 lastRot;
    [HideInInspector]
    public bool isSetLastRot;

    public void ClearOperationData()
    {
        addUpX = 0;
        addUpY = 0;
        oldX = 0;
        oldY = 0;
        accuX = 0;
        accuY = 0;
        isSetLastRot = false;
        curRot = Quaternion.identity;
    }

    public void Awake()
    {
#if UNITY_EDITOR
        isTouchInput = false;
        isMouseInput = true;
#endif
    }

    public void Update()
    {
        if (isUpdate)
        {
            MoveCamera();
        }

        isLockUpdate = false;
    }

    public void MoveCamera2()
    {
        if (isLockUpdate)
            return;
        isLockUpdate = false;

        MoveCamera();
        LerpRot();
    }

    private void MoveCamera()
    {
        float x = 0;
        float y = 0;
        if (isMouseInput)
        {
            x = Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
            y = Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
        }

        if (isTouchInput)
        {
            if (Input.touchCount > 0)
            {
                var tou = Input.touches[0];
                var origin = tou.deltaPosition;
                float dis = Vector2.Distance(lastPos, tou.position);
                if (dis < ignoreDistance)
                {
                    return;
                }

                Vector2 v2 = tou.position - lastPos;
                lastPos = tou.position;

                //Debug.Log("移动距离: " + dis);

                v2 = Vector2.ClampMagnitude(v2, 1);
                x = v2.x * xSpeed * Time.deltaTime;
                y = v2.y * ySpeed * Time.deltaTime;
                Debug.Log("touch操作 " + origin + "  nor:" + origin + "  Clamp:" + v2 + "  最终:" + x + " " + y);
                Debug.Log("rawPosition2 " + tou.rawPosition + "  position:" + tou.position);
            }
        }

        float oldAddUpX = addUpX;
        float oldAddUpY = addUpY;
        addUpX += x;
        addUpY += y;
//        Debug.Log("添加X和Y " + x + " " + y);

        if (isDebug)
        {
            PLGUITools.ShowUI("增量1 ", "x: " + x + "  " + "y: " + y);
            PLGUITools.ShowUI("增量2 ", "addUpX: " + addUpX + "  " + "addUpY: " + addUpY);
            PLGUITools.ShowUI("增量3 ", "xMinLimit: " + xMinLimit + "  " + "xMaxLimit: " + xMaxLimit);
            PLGUITools.ShowUI("增量4 ", "yMinLimit: " + yMinLimit + "  " + "yMaxLimit: " + yMaxLimit);
        }

        if (addUpX >= xMaxLimit || addUpX <= xMinLimit)
        {
            addUpX = oldAddUpX;
            x = 0;
            if (isDebug)
                PLGUITools.ShowUI(1, "还原 x " + addUpX);
            //Debug.Log("还原X");
        }

        if (addUpY >= yMaxLimit || addUpY <= yMinLimit)
        {
            addUpY = oldAddUpY;
            y = 0;
            if (isDebug)
                PLGUITools.ShowUI(2, "还原 y " + addUpY);

            //Debug.Log("还原Y");
        }

        accuX += Math.Abs(x);
        accuY += Math.Abs(y);

        var d = mainCamera.transform.up * y;
        var d2 = mainCamera.transform.right * x;
        var d3 = d + d2;
        var tempPos = mainCamera.transform.position;
        curPos += d3;

        if (isLimitPosition)
        {
            mainCamera.transform.position = Vector3.Lerp(tempPos, curPos, slerpValue);
        }

        if (isLimitRotation)
        {
            Vector3 eulerAngle = Vector3.zero;

            if (isSetLastRot)
                eulerAngle = lastRot;
            else
                eulerAngle = mainCamera.transform.rotation.eulerAngles;
            
            interpolation = eulerAngle;

            interpolation.x += -y;
            interpolation.y += x;
            //Debug.Log("");
            PLGUITools.ShowUI("四元数", interpolation);
            lastRot = interpolation;
            isSetLastRot = true;
        }
    }

    public void LerpRot()
    {
        if (!isSetLastRot)
            return;


        //有问题
        // var eulerAngle = mainCamera.transform.rotation.eulerAngles;
        // var newV = Vector2.Lerp(eulerAngle, lastRot, rotateSpeed * Time.deltaTime);
        // mainCamera.transform.rotation = Quaternion.Euler(newV);

        //四元数旋转
        var eulerAngle = mainCamera.transform.rotation.eulerAngles;
        var b = Quaternion.Euler(lastRot);
        mainCamera.transform.rotation =
            Quaternion.Slerp(mainCamera.transform.rotation, b, rotateSpeed * Time.deltaTime);

        //没问题
        //mainCamera.transform.rotation = Quaternion.Euler(lastRot);
    }
}